/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.pacs.controller;

import com.cxhd.pacs.DataConfig;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;

import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class ConfigCtrl extends Controller{
    
       public void index() {
        render("/admin/config.jsp");
    }

//    public void checkLogin() {
//        String password = getPara("pw");
//
//        if (DataConfig.getInstance().getLoginPW().equals(password)) {
//            renderText("true");
//        } else {
//            renderText("false");
//        }
//
//    }
//
//    public void configPage() {
//        setAttr("key", "true");
//        redirect("/dataConfig.jsp");
//    }

    public void saveConfig() {
        Map<String, String[]> map = getParaMap();
        System.out.println(DbKit.getConfig().getName());
        try {
           // Db.queryFirst("select * from config");
            for (String key : map.keySet()) {
                System.out.println("replace into config set name='" + key + "',value='" + getPara(key) + "'");
                Db.update("replace into config set name='" + key + "',value='" + getPara(key) + "'");
            }
            DataConfig.getInstance().initConfig();
           
            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
    }

    public void getConfig() {
     
       List<Record> list = Db.find("select * from config ");
        Map<String, String> map = new HashMap<String, String>();
        if (!list.isEmpty()) {
            for (Record re : list) {
                map.put(re.getStr("name"), re.getStr("value"));
            }
        }
        renderJson(map);
    }
    
}
